﻿using System;
using System.Xml.Linq;
using System.Xml.XPath;

namespace UtilZ.Dotnet.Ex.Log
{
    /// <summary>
    /// 日志操作公共类
    /// </summary>
    public static class LogUtil
    {
        /// <summary>
        /// 获取节点指定特性值
        /// </summary>
        /// <param name="ele"></param>
        /// <param name="attriName"></param>
        /// <returns></returns>
        public static string GetAttributeValue(this XElement ele, string attriName)
        {
            string value;
            var attri = ele.Attribute(attriName);
            if (attri != null)
            {
                value = attri.Value;
            }
            else
            {
                value = string.Empty;
            }

            return value;
        }

        /// <summary>
        /// 获取节点下指定名称子节点特性值
        /// </summary>
        /// <param name="ele"></param>
        /// <param name="childName"></param>
        /// <param name="attriName"></param>
        /// <returns></returns>
        public static string GetChildXElementValue(this XElement ele, string childName, string attriName = null)
        {
            string value;
            var childEle = ele.XPathSelectElement(string.Format(@"param[@name='{0}']", childName));
            if (childEle == null)
            {
                value = string.Empty;
            }
            else
            {
                if (string.IsNullOrWhiteSpace(attriName))
                {
                    attriName = "value";
                }

                value = GetAttributeValue(childEle, attriName);
            }

            return value;
        }

    }
}
